package com.github.shuaidd.service.mapper;

import com.github.shuaidd.model.repository.ModelHistoryEntityMapper;
import lombok.AllArgsConstructor;
import org.activiti.app.domain.editor.ModelHistory;
import org.activiti.engine.ManagementService;
import org.activiti.engine.impl.cmd.AbstractCustomSqlExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 描述 ACT_DE_MODEL_HISTORY
 *
 * @author ddshuai
 * @date 2020-07-18 11:58
 **/

@Service
@AllArgsConstructor(onConstructor_ = {@Autowired})
public class ModelHistoryEntityService extends BaseMapperService implements ModelHistoryEntityMapper {

    @Override
    public int deleteByPrimaryKey(String id) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelHistoryEntityMapper, Integer>(ModelHistoryEntityMapper.class) {
            @Override
            public Integer execute(ModelHistoryEntityMapper modelHistoryEntityMapper) {
                return modelHistoryEntityMapper.deleteByPrimaryKey(id);
            }
        });
    }

    @Override
    public int insert(ModelHistory record) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelHistoryEntityMapper, Integer>(ModelHistoryEntityMapper.class) {
            @Override
            public Integer execute(ModelHistoryEntityMapper modelHistoryEntityMapper) {
                return modelHistoryEntityMapper.insert(record);
            }
        });
    }

    @Override
    public int insertSelective(ModelHistory record) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelHistoryEntityMapper, Integer>(ModelHistoryEntityMapper.class) {
            @Override
            public Integer execute(ModelHistoryEntityMapper modelHistoryEntityMapper) {
                return modelHistoryEntityMapper.insertSelective(record);
            }
        });
    }

    @Override
    public ModelHistory selectByPrimaryKey(String id) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelHistoryEntityMapper, ModelHistory>(ModelHistoryEntityMapper.class) {
            @Override
            public ModelHistory execute(ModelHistoryEntityMapper modelHistoryEntityMapper) {
                return modelHistoryEntityMapper.selectByPrimaryKey(id);
            }
        });
    }

    @Override
    public int updateByPrimaryKeySelective(ModelHistory record) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelHistoryEntityMapper, Integer>(ModelHistoryEntityMapper.class) {
            @Override
            public Integer execute(ModelHistoryEntityMapper modelHistoryEntityMapper) {
                return modelHistoryEntityMapper.updateByPrimaryKeySelective(record);
            }
        });
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(ModelHistory record) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelHistoryEntityMapper, Integer>(ModelHistoryEntityMapper.class) {
            @Override
            public Integer execute(ModelHistoryEntityMapper modelHistoryEntityMapper) {
                return modelHistoryEntityMapper.updateByPrimaryKeyWithBLOBs(record);
            }
        });
    }

    @Override
    public int updateByPrimaryKey(ModelHistory record) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelHistoryEntityMapper, Integer>(ModelHistoryEntityMapper.class) {
            @Override
            public Integer execute(ModelHistoryEntityMapper modelHistoryEntityMapper) {
                return modelHistoryEntityMapper.updateByPrimaryKey(record);
            }
        });
    }

    @Override
    public List<ModelHistory> findByModelIdAndRemovalDateIsNullOrderByVersionDesc(String modelId) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelHistoryEntityMapper, List<ModelHistory>>(ModelHistoryEntityMapper.class) {
            @Override
            public List<ModelHistory> execute(ModelHistoryEntityMapper modelHistoryEntityMapper) {
                return modelHistoryEntityMapper.findByModelIdAndRemovalDateIsNullOrderByVersionDesc(modelId);
            }
        });
    }

    public List<ModelHistory> findByCreatedByAndModelTypeAndRemovalDateIsNull(String id, int modelTypeApp) {
        return null;
    }
}
